package com.yuelao.yuelao_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuelao.yuelao_backend.entity.SystemMessage;
import com.yuelao.yuelao_backend.enums.SystemMessageType;
import com.yuelao.yuelao_backend.mapper.SystemMessageMapper;
import com.yuelao.yuelao_backend.service.SystemMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 系统消息Service实现类
 */
@Service
public class SystemMessageServiceImpl extends ServiceImpl<SystemMessageMapper, SystemMessage> implements SystemMessageService {

    @Override
    public List<SystemMessage> getUserMessages(Long userId, int page, int size) {
        Page<SystemMessage> messagePage = new Page<>(page, size);
        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemMessage::getUserId, userId)
               .orderByDesc(SystemMessage::getCreatedAt);
        
        Page<SystemMessage> result = page(messagePage, wrapper);
        return result.getRecords();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markAsRead(Long messageId, Long userId) {
        SystemMessage message = getById(messageId);
        if (message == null || !message.getUserId().equals(userId)) {
            return false;
        }
        
        if (message.getIsRead() != null && message.getIsRead()) {
            return true; // 已读
        }
        
        message.setIsRead(true);
        return updateById(message);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markAllAsRead(Long userId) {
        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemMessage::getUserId, userId)
               .eq(SystemMessage::getIsRead, false);
        
        List<SystemMessage> unreadMessages = list(wrapper);
        
        for (SystemMessage message : unreadMessages) {
            message.setIsRead(true);
        }
        
        return updateBatchById(unreadMessages);
    }

    @Override
    public long getUnreadCount(Long userId) {
        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemMessage::getUserId, userId)
               .eq(SystemMessage::getIsRead, false);
        return count(wrapper);
    }

    @Override
    public long getUnreadCountByType(Long userId, String type) {
        LambdaQueryWrapper<SystemMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemMessage::getUserId, userId)
               .eq(SystemMessage::getType, type)
               .eq(SystemMessage::getIsRead, false);
        return count(wrapper);
    }

    @Override
    public boolean createMessage(Long userId, SystemMessageType type, String title, String content, Map<String, Object> extraData) {
        SystemMessage message = new SystemMessage();
        message.setUserId(userId);
        message.setType(type.getCode());
        message.setTitle(title);
        message.setContent(content);
        message.setExtraData(extraData);
        message.setIsRead(false);
        message.setIsImportant(false);
        message.setClickable(true);
        message.setCreatedAt(LocalDateTime.now());
        return save(message);
    }
}

